package cn.chenxiejia;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: Imguntil
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/24 18:05
 */
public class Imguntil {
    public static void main(String[] args) {
        shuiyin("陈卸甲", new File("2.png"), 4, Color.RED, "微软雅黑", 50f);
        try {
            Runtime.getRuntime().exec("cmd /k start 3.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void shuiyin(String text, File img2, int pos, Color color, String font1, float size) {
        try {
            var i2 = ImageIO.read(img2);
            var m = i2.createGraphics();
            m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
            int x = 0;
            int y = 0;
            int sp = 10;
            Font font = new Font(font1, Font.BOLD, 20);
            var fm = m.getFontMetrics(font);
            Random rand = new Random();
            switch (pos) {
                case 1:
                    x = sp;
                    y = fm.getDescent() + sp;
                    break;

                case 2:
                    x = i2.getWidth() / 2 - fm.stringWidth(text) / 2;
                    y = fm.getDescent() + sp;
                    break;

                case 3:
                    x = i2.getWidth() - fm.stringWidth(text) - sp;
                    y = fm.getDescent() + sp;
                    break;

                case 4:
                    x = sp;
                    y = i2.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2;
                    break;

                case 5:
                    x = i2.getWidth() / 2 - fm.stringWidth(text) / 2;
                    y = i2.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2;
                    break;

                case 6:
                    x = i2.getWidth() - fm.stringWidth(text) - sp;
                    y = i2.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2;
                    break;

                case 7: {
                    x = sp;
                    y = i2.getHeight() - fm.getDescent() - sp;
                    break;

                }
                case 8:
                    x = i2.getWidth() / 2 - fm.stringWidth(text) / 2;
                    y = i2.getHeight() - fm.getDescent() - sp;
                    break;

                case 9:
                    x = i2.getWidth() - fm.stringWidth(text) - sp;
                    y = i2.getHeight() - fm.getDescent() - sp;
                    break;

                default:
                    x = rand.nextInt(i2.getWidth());
                    y = rand.nextInt(i2.getHeight());

            }
            m.setColor(color);
            font = font.deriveFont(size);
            m.setFont(font);
            m.drawString(text, x, y);
            m.dispose();
            ImageIO.write(i2, "png", new File("3.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
